package com.roncoo.pay.permission.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.roncoo.pay.common.core.page.PageBean;
import com.roncoo.pay.common.core.page.PageParam;
import com.roncoo.pay.permission.entity.PmsPermission;
import com.roncoo.pay.permission.entity.PmsRolePermission;
import com.roncoo.pay.permission.repository.PmsPermissionRepository;
import com.roncoo.pay.permission.repository.PmsRolePermissionRepository;
import com.roncoo.pay.permission.service.PmsPermissionService;

/**
 * 权限service接口实现
 *
 */
@Service("pmsPermissionService")
public class PmsPermissionServiceImpl implements PmsPermissionService {
	@Autowired
	private PmsPermissionRepository pmsPermissionRepository;
	@Autowired
	private PmsRolePermissionRepository pmsRolePermissionRepository;

	/**
	 * 创建pmsOperator
	 */
	public void saveData(PmsPermission pmsPermission) {
		pmsPermissionRepository.save(pmsPermission);
	}

	/**
	 * 修改pmsOperator
	 */
	public void updateData(PmsPermission pmsPermission) {
		pmsPermissionRepository.save(pmsPermission);
	}

	/**
	 * 根据id获取数据pmsOperator
	 * 
	 * @param id
	 * @return
	 */
	public PmsPermission getDataById(String id) {
		return pmsPermissionRepository.findOne(id);

	}

	/**
	 * 分页查询pmsOperator
	 * 
	 * @param pageParam
	 * @param ActivityVo
	 *            PmsOperator
	 * @return
	 */
	public PageBean<PmsPermission> listPage(PageParam pageParam, PmsPermission pmsPermission) {
		ExampleMatcher eMatcher = ExampleMatcher.matching();
		if(StringUtils.isNotEmpty(pmsPermission.getPermissionName())){
			eMatcher.withMatcher("permissionName", GenericPropertyMatchers.contains());
		}else{
			pmsPermission.setPermissionName(null);
		}
		if(StringUtils.isNotEmpty(pmsPermission.getPermission())){
			eMatcher.withMatcher("permission", GenericPropertyMatchers.storeDefaultMatching());
		}else{
			pmsPermission.setPermission(null);
		}
		eMatcher.withIgnorePaths("version");
		pmsPermission.setCreater(null);
		pmsPermission.setCreateTime(null);
		pmsPermission.setEditor(null);
		pmsPermission.setEditTime(null);
		pmsPermission.setRemark(null);
		pmsPermission.setStatus(null);
		pmsPermission.setId(null);
		Example<PmsPermission> ex = Example.of(pmsPermission,eMatcher);
		
		Pageable pg = new PageRequest(pageParam.getPageNum(), pageParam.getNumPerPage());
		
		Page<PmsPermission> page = pmsPermissionRepository.findAll(ex, pg);
		
		PageBean<PmsPermission> pb = new PageBean<PmsPermission>(pageParam.getPageNum(), pageParam.getNumPerPage(), page.getTotalPages(),page.getContent());
		return pb;
	}

	/**
	 * 检查权限名称是否已存在
	 * 
	 * @param trim
	 * @return
	 */
	public PmsPermission getByPermissionName(String permissionName) {
		return pmsPermissionRepository.findByPermissionName(permissionName);
	}

	/**
	 * pmsPermissionDao 删除
	 * 
	 * @param permission
	 */
	public void delete(String permissionId) {
		pmsPermissionRepository.delete(permissionId);
	}

	/**
	 * 根据角色查找角色对应的功能权限ID集
	 * 
	 * @param roleId
	 * @return
	 */
	public String getPermissionIdsByRoleId(String roleId) {
		List<PmsRolePermission> rmList = pmsRolePermissionRepository.findByRoleId(roleId);
		StringBuffer actionIds = new StringBuffer();
		if (rmList != null && !rmList.isEmpty()) {
			for (PmsRolePermission rm : rmList) {
				actionIds.append(rm.getPermissionId()).append(",");
			}
		}
		return actionIds.toString();
	}

	/**
	 * 查询所有的权限
	 */
	public List<PmsPermission> listAll() {
		return pmsPermissionRepository.findAll();
	}
}
